package com.jonluo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jonluo.exception.RReturnException;
import com.jonluo.model.SysRoleMenu;
import com.jonluo.service.SysRoleMenuService;
import com.jonluo.utils.PageInfo;
import com.jonluo.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author jonluo
 * Date 2017/9/15
 */
@RestController
@RequestMapping(value = "/roleMenu")
@Api(description = "角色菜单服务api")
public class SysRoleMenuController {

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @ApiOperation(value = "获取角色菜单列表", notes = "分页查询角色菜单", response = Map.class)
    @ApiImplicitParam(name = "jsonObject", value = "json对象参数 例子{\"page\":1,\"limit\":10,\"params\":" +
            "{\"mobile\":\"123\",\"username\":\"ljw\"} }", required = true, dataType = "JsonObject")
    @RequestMapping(value = {"/getAll"}, method = RequestMethod.POST)
    public ResponseEntity getAll(@RequestBody JSONObject jsonObject) {
        R r = null;
        PageInfo pageInfo = new PageInfo(jsonObject);
        JSONObject json = new JSONObject();
        List<SysRoleMenu> list = sysRoleMenuService.queryList(pageInfo);
        Integer total = sysRoleMenuService.queryTotal(pageInfo);
        json.put("total", total);
        json.put("data", list);
        r = R.ok(json);
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "获取角色菜单", notes = "根据id查询角色菜单", response = Map.class)
    @ApiImplicitParam(name = "id", value = "角色菜单id", paramType = "path", required = true, dataType = "String")
    @RequestMapping(value = {"/getById/{id}"}, method = RequestMethod.GET)
    public ResponseEntity getById(@PathVariable String id) {
        R r = null;
        JSONObject json = new JSONObject();
        SysRoleMenu sysRoleMenu = sysRoleMenuService.query(id);
        json.put("data", sysRoleMenu);
        r = R.ok(json);
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "添加角色菜单", notes = "添加角色菜单信息", response = Map.class)
    @ApiImplicitParam(name = "jsonObject", value = "json对象参数 {userId,username,password,salt,email,mobile,status,deptId,createTime}", required = true, dataType = "JsonObject")
    @RequestMapping(value = {"/add"}, method = RequestMethod.POST)
    public ResponseEntity add(@RequestBody JSONObject jsonObject) {
        R r = null;
        SysRoleMenu sysRoleMenu = JSON.parseObject(jsonObject.toJSONString(), SysRoleMenu.class);
        Integer sum = sysRoleMenuService.save(sysRoleMenu);
        if (sum > 0) {
            r = R.ok();
        } else {
            throw new RReturnException("添加角色菜单失败");
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "批量添加角色菜单", notes = "批量添加角色菜单", response = Map.class)
    @ApiImplicitParam(name = "jsonObject", value = "json对象参数 {userId,username,password,salt,email,mobile,status,deptId,createTime}", required = true, dataType = "JsonObject")
    @RequestMapping(value = {"/addBatch"}, method = RequestMethod.POST)
    public ResponseEntity addBatch(@RequestBody JSONObject jsonObject) {
        R r = null;
        List<SysRoleMenu> list =  JSONArray.parseArray(jsonObject.getJSONArray("list").toJSONString(),SysRoleMenu.class);
        Integer sum = sysRoleMenuService.saveBatch(list);
        if (sum  == list.size()) {
            r = R.ok();
        } else {
            throw new RReturnException("批量添加角色菜单失败");
        }
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value = "修改角色菜单", notes = "根据id修改角色菜单信息", response = Map.class)
    @ApiImplicitParam(name = "jsonObject", value = "json对象参数 {对象实体}", required = true, dataType = "JsonObject")
    @RequestMapping(value = {"/update"}, method = RequestMethod.POST)
    public ResponseEntity update(@RequestBody JSONObject jsonObject) {
        R r = null;
        SysRoleMenu sysRoleMenu = JSON.parseObject(jsonObject.toJSONString(), SysRoleMenu.class);
        Integer sum = sysRoleMenuService.update(sysRoleMenu);
        if (sum > 0) {
            r = R.ok();
        } else {
            throw new RReturnException("更新角色菜单失败");
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "删除角色菜单", notes = "批量删除角色菜单", response = Map.class)
    @ApiImplicitParam(name = "jsonObject", value = "json对象参数 例子{\"ids\":\"id1,id2\"}多个id以逗号隔开", required = true, dataType = "JsonObject")
    @RequestMapping(value = {"/deleteByIds"}, method = RequestMethod.POST)
    public ResponseEntity deleteByIds(@RequestBody JSONObject jsonObject) {
        R r = null;
        String[] ids= jsonObject.getString("ids").split(",");
        Integer sum = sysRoleMenuService.deleteBatch(ids);
        if (sum == ids.length) {
            r = R.ok();
        } else {
            throw new RReturnException("批量删除角色菜单失败");
        }
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value = "根据角色ids批量删除角色菜单", notes = "根据角色ids批量删除角色菜单", response = Map.class)
    @ApiImplicitParam(name = "jsonObject", value = "json对象参数 例子{\"roleIds\":\"id1,id2\"}", required = true, dataType = "JsonObject")
    @RequestMapping(value = {"/deleteByRoleIdsOrMenuIds"}, method = RequestMethod.POST)
    public ResponseEntity deleteByRoleIdsOrMenuIds(@RequestBody JSONObject jsonObject) {
        R r = null;
        String[] menuIds= null;
        String[] roleIds=null;
        if (StringUtils.isNotBlank(jsonObject.getString("menuIds"))){
            menuIds = jsonObject.getString("menuIds").split(",");
        }
        if (StringUtils.isNotBlank(jsonObject.getString("roleIds"))){
            roleIds = jsonObject.getString("roleIds").split(",");
        }
        Integer sum = sysRoleMenuService.deleteByRoleIdsOrMenuIds(roleIds,menuIds);
        if (sum >= 0) {
            r = R.ok();
        } else {
            throw new RReturnException("根据角色ids批量删除角色菜单失败");
        }
        return ResponseEntity.ok(r);
    }
}
